﻿using System.ComponentModel;
using UnityEngine;

namespace Assets.Scripts.AI
{
    public class Agent : MonoBehaviour
    {
        /// <summary>
        /// 最大速度
        /// </summary>
        public float maxSpeed;
        /// <summary>
        /// 最大加速度
        /// </summary>
        public float maxAccel;
        public float orientation;
        public float rotation;
        [DisplayOnly]
        public Vector3 velocity;
        [SerializeField]
        protected Steering steering;

        private Rigidbody aRigdbody;

        private void Start()
        {
            velocity = Vector3.zero;
            steering = new Steering();
            aRigdbody = GetComponent<Rigidbody>();
        }

        public void SetSteering(Steering steering)
        {
            this.steering.linear = steering.linear;
            this.steering.angular = steering.angular;
        }

        private void FixedUpdate()
        {
            if (aRigdbody == null)
                return;

            Vector3 displacement = velocity * Time.deltaTime;
            orientation += rotation * Time.deltaTime;
            if (orientation < 0.0f)
            {
                orientation += 360.0f;
            }
            else if (orientation > 360.0f)
            {
                orientation -= 360.0f;
            }

            aRigdbody.AddForce(displacement, ForceMode.VelocityChange);
            Vector3 orientationVector = OriToVec(orientation);
            aRigdbody.rotation = Quaternion.LookRotation(orientationVector, Vector3.up);
        }

        public virtual void Update()
        {
            if (aRigdbody == null)
                return;

            float delta = Time.deltaTime;
            Vector3 displacement = velocity * delta;
            orientation += rotation * delta;
            if (orientation < 0.0f)
            {
                orientation += 360.0f;
            }
            else if (orientation > 360.0f)
            {
                orientation -= 360.0f;
            }

            transform.Translate(displacement, Space.World);
            transform.rotation = Quaternion.identity;
            transform.Rotate(Vector3.up, orientation);
        }

        public virtual void LateUpdate()
        {
            if (aRigdbody == null)
                return;

            float delta = Time.deltaTime;
            velocity += steering.linear * delta;
            rotation += steering.angular * delta;

            if (velocity.magnitude > maxSpeed)
            {
                velocity.Normalize();
                velocity *= maxSpeed;
            }

            if (steering.angular == 0.0f)
            {
                rotation = 0.0f;
            }

            if (steering.linear.sqrMagnitude == 0.0f)
            {
                velocity = Vector3.zero;
            }

            //steering = new Steering();
        }

        public Vector3 OriToVec(float orientation)
        {
            Vector3 vector = Vector3.zero;
            vector.x = Mathf.Sin(orientation * Mathf.Deg2Rad) * 1.0f;
            vector.y = Mathf.Cos(orientation * Mathf.Deg2Rad) * 1.0f;
            return vector.normalized;
        }
    }
}